﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI.WebControls;

namespace GPCSC.Webs
{
    /// <summary>
    /// 分页类
    /// </summary>
    public class FYpage
    {
        public FYpage()
        { 
        
        }
       /// <summary>
       /// 分页方法
       /// </summary>
       /// <param name="dt"> 数据源 table</param>
       /// <param name="webcontrol"> 绑定数据的控件</param>
       /// <param name="pagesize"> 每页显示多少条记录</param>
       /// <returns></returns>
        public string GetPageNum(DataTable dt, Object webcontrol, int pagesize)
        {
            try
            {
                PagedDataSource objPds = new PagedDataSource();
                objPds.DataSource = dt.DefaultView;
                objPds.AllowPaging = true;
                int total = dt.Rows.Count;
                objPds.PageSize = pagesize;
                int page = 1;
                string Url = HttpContext.Current.Request.Url.PathAndQuery;//路径
                string Query = Url.Substring(Url.IndexOf("aspx") + 4);//aspx后的字符串
                if (Query == "")
                {
                    Query += "?";
                }
                else
                {
                    if (Usual.f_requestParamsToString("page") != "")
                    {
                        if (Usual.f_convertInt32(Usual.f_requestParamsToString("page")) > 0)
                        {
                            page = Usual.f_convertInt32(Usual.f_requestParamsToString("page"));
                        }
                        else
                        {
                            page = 1;
                        }

                        Query = Query.Substring(0, Query.LastIndexOf("page"));
                    }
                    else
                    {
                        Query += "&";
                        page = 1;
                    }

                }


                int allpage = 0;
                int next = 0;
                int pre = 0;
                int startcount = 0;
                int endcount = 0;
                string pagestr = "";
                if (page < 1) { page = 1; }
                //计算总页数
                if (pagesize != 0)
                {
                    allpage = (total / pagesize);
                    allpage = ((total % pagesize) != 0 ? allpage + 1 : allpage);
                    allpage = (allpage == 0 ? 1 : allpage);
                    if (allpage < page)
                    {
                        page = allpage;
                    }
                }
                objPds.CurrentPageIndex = page - 1;
                string type = webcontrol.GetType().Name;
                switch (type)
                {
                    case "Repeater":
                        ((Repeater)webcontrol).DataSource = objPds;
                        ((Repeater)webcontrol).DataBind();
                        break;
                    case "DataList":
                        ((DataList)webcontrol).DataSource = objPds;
                        ((DataList)webcontrol).DataBind();
                        break;
                    case "GridView":
                        ((GridView)webcontrol).DataSource = objPds;
                        ((GridView)webcontrol).DataBind();
                        break;
                }
                next = page + 1;
                pre = page - 1;
                startcount = (page + 5) > allpage ? allpage - 9 : page - 4;//中间页起始序号
                //中间页终止序号
                endcount = page < 5 ? 10 : page + 5;
                if (startcount < 1) { startcount = 1; } //为了避免输出的时候产生负数，设置如果小于1就从序号1开始
                if (allpage < endcount) { endcount = allpage; } //页码+5的可能性就会产生最终输出序号大于总页码，那么就要将其控制在页码数之内

                pagestr += page > 1 ? "<a href='#' onclick='cPage(\"1\",\"CompName\")'>首页</a>  <a href='#' onclick='cPage(\"" + pre + "\",\"CompName\")'  >上一页</a> " : " 首页 上一页 ";
                //中间页处理，这个增加时间复杂度，减小空间复杂度
                for (int i = startcount; i <= endcount; i++)
                {
                    pagestr += page == i ? "  <font color='#ff0000'>" + i + "</font>" : "  <a  href='#' onclick='cPage(\"" + i + "\",\"CompName\")'>" + i + "</a>";
                }
                pagestr += page != allpage ? "  <a href='#' onclick='cPage(\"" + next + "\",\"CompName\")'>下一页</a>  <a href='#' onclick='cPage(\"" + allpage + "\",\"CompName\")' >末页</a>" : " 下一页  末页";
                pagestr += " <SPAN >每页" + pagesize.ToString() + "条记录/当前第" + page.ToString() + "页/共" + allpage + "页/共" + dt.Rows.Count + "条记录";
                pagestr += " 到第<INPUT id='pageJumpto'  name='jumpto' value='" + page.ToString() + "'  />页<input type=\"button\"  class=\"selectsbutton\" onclick='chagepage(\"CompName\")' value=\"跳转\"  /> </SPAN> ";

                return pagestr;
            }
            catch (Exception ee)
            {
                throw ee;
            }
        }
    }
}